/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gbbmgmt;

import java.util.*;
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;

/**
 *
 * @author dchapman
 */
public class ConfigOps {
    public void ReadConfig() {
        Properties configFile = new Properties();
        URL propsURL = null;
        try{
            propsURL = new URL("File:///" + System.getProperty("user.dir") + File.separator + "GBB.properties");
        } catch (MalformedURLException me){
            me.printStackTrace();
        }

        try {
            configFile.load(propsURL.openStream());
            Global.dbURL = configFile.getProperty("URL");
            Global.dbName = configFile.getProperty("DBNAME");
            Global.passWord = configFile.getProperty("PASSWORD");
            Global.userName = configFile.getProperty("USERNAME");
            Global.dbPort = configFile.getProperty("PORT");
            Global.COTax = Double.valueOf(configFile.getProperty("COTAX").trim()).doubleValue();
            Global.TTBTax = Double.valueOf(configFile.getProperty("TTBTAX").trim()).doubleValue();
            Global.TwentyTwoCase = Integer.valueOf(configFile.getProperty("TTCASE").trim()).intValue();
            Global.TwelveCase = Integer.valueOf(configFile.getProperty("TWCASE").trim()).intValue();
            Global.SevenFiftyCase = Integer.valueOf(configFile.getProperty("SFCASE").trim()).intValue();
        } catch(java.io.IOException exp) {
            //exp.printStackTrace();
        }
    }

    public void UpdateDBConfig(){
        Properties configFile = new Properties();
        Properties p = new Properties();
        URL propsURL = null;
        try{
            propsURL = new URL("File:///" + System.getProperty("user.dir") + File.separator + "GBB.properties");
        } catch (MalformedURLException me){
            me.printStackTrace();
        }

        try {
            configFile.load(propsURL.openStream());
            configFile.setProperty("URL", Global.dbURL);
            configFile.setProperty("DBNAME", Global.dbName);
            configFile.setProperty("PASSWORD", Global.passWord);
            configFile.setProperty("USERNAME", Global.userName);
            configFile.setProperty("PORT", Global.dbPort);
            configFile.setProperty("COTAX", Double.toString(Global.COTax));
            configFile.setProperty("TTBTAX", Double.toString(Global.TTBTax));
            configFile.setProperty("TTCASE", Integer.toString(Global.TwentyTwoCase));
            configFile.setProperty("TWCASE", Integer.toString(Global.TwelveCase));
            configFile.setProperty("SFCASE", Integer.toString(Global.SevenFiftyCase));
            configFile.store(new FileOutputStream(propsURL.getPath()), null);
        } catch(java.io.IOException exp) {
            exp.printStackTrace();
        }
    }

    public String getClassName() {
        String thisClassName;

        //Build a string with executing class's name.
        thisClassName = this.getClass().getName();
        thisClassName = thisClassName.substring(thisClassName.lastIndexOf(".") + 1, thisClassName.length());
        thisClassName += ".class";

        return thisClassName;
    }

public String getLocalDirName()
   {
      String localDirName;

      //Use that name to get a URL to the directory we are executing in
      java.net.URL myURL = this.getClass().getResource(getClassName());  //Open a URL to the our .class file

      //Clean up the URL and make a String with absolute path name
      localDirName = myURL.getPath();  //Strip path to URL object out
      localDirName = myURL.getPath().replaceAll("%20", " ");  //change %20 chars to spaces

      //Get the current execution directory
      localDirName = localDirName.substring (0,localDirName.lastIndexOf("/"));  //clean off the file name

      return localDirName;
   }
}
